﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Skin2D.Bone;
using Skin2D.Control.LogSystem;

namespace Skin2D.Control.Cursor
{
    class SelectCursor : ICursor
    {
        public SelectCursor(ToolStripButton button)
            : base(button)
        {
            this.Type = CurrsorType.Select;
        }

        public override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && m_selected != null)
            {
                m_last_point = new PointF(e.X, e.Y);
            }

        }

        public override void OnMouseMove(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && m_selected != null)
            {
                PointF point = new PointF(e.X, e.Y);

                float translate_x = m_last_point.X - point.X;
                float translate_y = m_last_point.Y - point.Y;

                PointF trans = m_selected.RelativeTranslate;
                m_selected.setTranslate(new PointF(trans.X - translate_x, trans.Y - translate_y));

                m_last_point = point;

                Program.form.Cursor = Cursors.Cross;
            }
        }

        public override void OnMouseUp(MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
                return;

            Program.form.Cursor = Cursors.Default;

            PointF point = new PointF(e.X, e.Y);
            this.TransfromToCamera(ref point);

            //Check Modify Last selected
            {
                if (m_selected !=null && m_last_translate.X != Int32.MinValue && m_last_translate.Y != Int32.MinValue)
                {
                    if(m_selected.RelativeTranslate.X != m_last_translate.X || m_selected.RelativeTranslate.Y != m_last_translate.Y)
                        EventLog.AddLog("RelativeTranslate", m_selected, m_last_translate);
                }
            }

            m_selected = null;
            m_last_translate = new PointF(Int32.MinValue, Int32.MinValue);
            foreach (Bone.Bone2D item in MyApplication.m_BoneListTreeView.m_TreeNodeMap.Keys)
            {
                if (item.getBoundBox().Contains(point))
                {
                    MyApplication.m_BoneListTreeView.Select(item);
                    m_selected = item;
                    m_last_translate = m_selected.RelativeTranslate;
                    return;
                }
            }

            MyApplication.m_BoneListTreeView.Select(null);
        }

        private Bone2D m_selected;
        private PointF m_last_point;
        private PointF m_last_translate;
    }
}
